home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / world / shadow / shadow1.java < prev    next >
Text File  |  1996-10-17  |  4KB  |  243 lines

  1. // "Motion Shadow Effect"
  2.  
  3. //     created by ask@krc.sony.co.jp (Masamichi zzzcat Asukai)
  4.  
  5. //
  6.  
  7. // Copyright(C) 1996 Sony Corporation. All rights reserved.
  8.  
  9. //
  10.  
  11.  
  12.  
  13. import vrml.*;
  14.  
  15. import vrml.node.*;
  16.  
  17. import vrml.field.*;
  18.  
  19.  
  20.  
  21. public class shadow1 extends Script {
  22.  
  23.  
  24.  
  25.     private Node light;
  26.  
  27.     private Node object;
  28.  
  29.     private boolean shadow_length;
  30.  
  31.     private boolean shadow_depth;
  32.  
  33.  
  34.  
  35.     private SFVec3f lightTranslation;
  36.  
  37.     private SFVec3f objectTranslation;
  38.  
  39.     private SFVec3f objectScale;
  40.  
  41.     private SFRotation shadowRotation;
  42.  
  43.     private SFVec3f shadowScale;
  44.  
  45.     private SFFloat shadowTransparency;
  46.  
  47.  
  48.  
  49.     private float[] l_trans = new float[3];
  50.  
  51.     private float[] o_trans = new float[3];
  52.  
  53.     private float[] o_scale = new float[3];
  54.  
  55.  
  56.  
  57.     private float[] s_rot = {0.0f, 1.0f, 0.0f, 0.0f};
  58.  
  59.     private float[] s_scale = {1.0f, 1.0f, 1.0f};
  60.  
  61.     private float s_transparency;
  62.  
  63.  
  64.  
  65.     private float[] vec = new float[3];
  66.  
  67.     private float d;
  68.  
  69.  
  70.  
  71.  
  72.  
  73.     public void initialize() {
  74.  
  75.  
  76.  
  77.     // get field values of script node
  78.  
  79.     light = (Node)((SFNode)getField("light")).getValue();
  80.  
  81.     object = (Node)((SFNode)getField("object")).getValue();
  82.  
  83.     shadow_length = 
  84.  
  85.         (boolean)((SFBool)getField("shadow_length")).getValue();
  86.  
  87.     shadow_depth = 
  88.  
  89.         (boolean)((SFBool)getField("shadow_length_and_depth")).getValue();
  90.  
  91.  
  92.  
  93.     // get exposed fields
  94.  
  95.     lightTranslation = (SFVec3f)light.getExposedField("translation");
  96.  
  97.     objectTranslation = (SFVec3f)object.getExposedField("translation");
  98.  
  99.     objectScale = (SFVec3f)object.getExposedField("scale");
  100.  
  101.     shadowRotation = (SFRotation)object.getExposedField("shadow_rotation");
  102.  
  103.     shadowScale = (SFVec3f)object.getExposedField("shadow_scale");
  104.  
  105.     shadowTransparency = (SFFloat)object.getExposedField("shadow_transparency");
  106.  
  107.     }
  108.  
  109.  
  110.  
  111.     
  112.  
  113.     public void processEvent(Event e) {
  114.  
  115.     if (e.getName().equals("interval")) {
  116.  
  117.  
  118.  
  119.         lightTranslation.getValue(l_trans);
  120.  
  121.         objectTranslation.getValue(o_trans);
  122.  
  123.         objectScale.getValue(o_scale);
  124.  
  125.  
  126.  
  127.         ////////////////////
  128.  
  129.         // SHADOW ROTATION
  130.  
  131.         ////////////////////
  132.  
  133.  
  134.  
  135.         // normalized light vector on X-Z plane
  136.  
  137.         vec[0] = o_trans[0] - l_trans[0];
  138.  
  139.         vec[2] = o_trans[2] - l_trans[2];
  140.  
  141.         d = (float)java.lang.Math.sqrt(vec[0]*vec[0] + vec[2]*vec[2]);
  142.  
  143.         vec[0] /= d;
  144.  
  145.         vec[2] /= d;
  146.  
  147.  
  148.  
  149.         // rotation of shadow
  150.  
  151.         if (vec[0] < 0.0) {
  152.  
  153.             s_rot[3] = (float)java.lang.Math.acos(-vec[2]);
  154.  
  155.         } else {
  156.  
  157.             s_rot[3] = -(float)java.lang.Math.acos(-vec[2]);
  158.  
  159.         }
  160.  
  161.  
  162.  
  163.         // set rotation of shadow
  164.  
  165.             shadowRotation.setValue(s_rot);
  166.  
  167.  
  168.  
  169.         ////////////////////
  170.  
  171.         // SHADOW LENGTH
  172.  
  173.         ////////////////////
  174.  
  175.  
  176.  
  177.         if (true == shadow_length || true == shadow_depth) {
  178.  
  179.             // whether light height is higher than top of object
  180.  
  181.             if (l_trans[1] < o_scale[1]) {
  182.  
  183.                 s_scale[2] = 0.0f;
  184.  
  185.             } else {
  186.  
  187.                 s_scale[2] = d / (l_trans[1] - o_scale[1]);
  188.  
  189.             }
  190.  
  191.         }
  192.  
  193.         if (true == shadow_length) {
  194.  
  195.             // set length of shadow
  196.  
  197.                 shadowScale.setValue(s_scale);
  198.  
  199.         }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.         ////////////////////
  206.  
  207.         // SHADOW DEPTH
  208.  
  209.         ////////////////////
  210.  
  211.  
  212.  
  213.         if (true == shadow_depth) {
  214.  
  215.             if (s_scale[2] < 1.0f) {
  216.  
  217.                 s_transparency = 0.0f;
  218.  
  219.             } else {
  220.  
  221.                 s_transparency = 1.0f / s_scale[2];
  222.  
  223. //                s_transparency = 1.0f / (s_scale[2] * s_scale[2]);
  224.  
  225.                 s_transparency = 1.0f - s_transparency;
  226.  
  227.             }
  228.  
  229.  
  230.  
  231.             // set depth of shadow
  232.  
  233.                 shadowTransparency.setValue(s_transparency);
  234.  
  235.         }
  236.  
  237.         }
  238.  
  239.     }
  240.  
  241. }
  242.  
  243.